package com.itcast.mallweb.controller;

import com.itcast.admin.dto.AdminDTO;
import com.itcast.admin.dto.CommodityDTO;
import com.itcast.admin.dto.CommondityAmendDTO;
import com.itcast.admin.entity.Admin;
import com.itcast.admin.vo.AdminLoginVO;
import com.itcast.admin.vo.DityVO;
import com.itcast.applet.entity.Commodity;
import com.itcast.constant.JwtClaimsConstant;
import com.itcast.constant.MessageConstant;
import com.itcast.mallweb.service.CommodityService;
import com.itcast.result.Result;
import com.itcast.utils.AliOssUtil;
import com.itcast.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.login.AccountNotFoundException;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/admin/dity")
@Slf4j
@Api(tags = "web-商品相关接口")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private AliOssUtil aliOssUtil;

    //接口已通过-OK
    @PostMapping("/add")
    @ApiOperation("新增商品接口")
    @CacheEvict(cacheNames = "setmealCache", key = "#commodityDTO")
    public Result<String> dityAdd(CommodityDTO commodityDTO, @RequestParam("image") MultipartFile[] files) {
        log.info("新增的商品信息为：{}", commodityDTO);
        log.info("文件上传信息：{}", files);
        commodityService.dityAdd(commodityDTO, files);
        return Result.success();
    }

    //接口已通过-OK
    @GetMapping("/query")
    @ApiOperation("查询单个商品")
    public Result<DityVO> dityQuery(@RequestParam Integer id) {
        log.info("查询单个商品的id为：{}", id);
        DityVO dityVO = commodityService.dityQuery(id);
        return Result.success(dityVO);
    }

    //接口已通过-OK
    @GetMapping("/queryAll")
    @ApiOperation("查询全部商品")
    public Result<List<Commodity>> dityAllQuery() {
        log.info("查询全部商品");
        List<Commodity> dityVOlist = commodityService.dityAllQuery();
        return Result.success(dityVOlist);
    }

    //接口已通过-OK
    @PostMapping("/detele")
    @ApiOperation("批量删除商品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result dityDetele(@RequestBody List<Integer> ids) {
        log.info("删除商品的id为：{}", ids);
        commodityService.dityDetele(ids);
        return Result.success();
    }

    //接口已通过-OK
    @PostMapping("/amend")
    @ApiOperation("修改商品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result dityAmend(CommondityAmendDTO commondityAmendDTO, MultipartFile[] image) {
        log.info("修改商品的信息为：{}", commondityAmendDTO);
        log.info("文件上传的信息：{}", image);
        if (image != null && image.length > 0) {
            List<String> dityUrl = new ArrayList<>();
            for (MultipartFile file : image) {
                try {
                    //原始文件名
                    String originalFilename = file.getOriginalFilename();
                    //截取原始文件名的后缀 37-57cj.png
                    String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//截取最后一个点的后半部分
                    //构建新文件名称
                    String objectName = UUID.randomUUID().toString() + extension;
                    //文件的请求路径
                    String upload = aliOssUtil.upload(file.getBytes(), objectName); //把这个返回给前端
                    dityUrl.add(upload);
                } catch (IOException e) {
                    log.error("文件上传失败：{}", e);
                }

            }
            commondityAmendDTO.setAvatar(dityUrl);
        }
        commodityService.dityAmend(commondityAmendDTO);
        return Result.success();
    }

}

